package com.hk.core.authentication.api.menus;

import com.hk.commons.util.JsonUtils;
import com.hk.core.authentication.api.SecurityContextUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;


/**
 * @author Kevin
 */
@Slf4j
@RequiredArgsConstructor
public abstract class AbstractMenuManager implements MenuManager {

    protected abstract List<Menu> getAllMenu();

    @Override
    public List<Menu> getCurrentUserMenu() {
        var principal = SecurityContextUtils.getPrincipal();
        if (principal.isAdministrator()) {
            log.trace("The current login user [{}] is a system administrator", principal.getAccount());
            return getAllMenu();
        }
        var roles = principal.getRoles();
        var userPermissions = principal.getPermissions();
        log.trace("account:{},roles:{},permissions:{}", principal.getAccount(),
                JsonUtils.serialize(roles), JsonUtils.serialize(userPermissions));
        return MenuManager.filterRolesAndPerms(getAllMenu(), roles, userPermissions);
    }


}
